﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using TalentDesk.Contracts.Ef;
using TalentDesk.Contracts.ServiceContracts;
using TalentDesk.Contracts.DataContracts;
using TalentDesk.WcfService.EFQueries;
using TalentDesk.Data;

namespace TalentDesk.WcfService.Implementation
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    internal class AttachmentService : ProjectBaseClass, IAttachmentService
    {


        public List<Attachment> Get()
        {
            return TalentDeskContext.Attachments.AsEnumerable().ToList();
        }

        public List<Attachment> GetAttachmentsByProjectId(int p_iProjectId)
        {
            return TalentDeskContext.Attachments.Where(p => p.ProjectId == p_iProjectId).AsEnumerable().ToList();
        }

        public void SaveAttachmentList(List<Attachment> p_lAttachments)
        {
            try
            {
                BeginTransaction();
                foreach (var item in p_lAttachments)
                {
                    TalentDeskContext.Attachments.ApplyChanges(item);
                }

                TalentDeskContext.SaveChanges();
                CommitTransaction();
            }
            catch (Exception ex)
            {
                RollbackTransaction();
                throw ex;
            }
        }

        public void SaveAttachment(Attachment p_oAttachments)
        {
            try
            {
                BeginTransaction();

                TalentDeskContext.Attachments.ApplyChanges(p_oAttachments);


                TalentDeskContext.SaveChanges();
                CommitTransaction();
            }
            catch (Exception ex)
            {
                RollbackTransaction();
                throw ex;
            }
        }

        public Attachment GetAttachmentsByAttachmentId(int p_iAttachmentId)
        {
            return TalentDeskContext.Attachments.Where(p => p.AttachmentId == p_iAttachmentId).AsEnumerable().FirstOrDefault();
        }
    }
}
